package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验器的步骤
 * 1) 编写校验注解 ListValid
 * 2) 编写校验器 ListValidConstraintValidator
 * 3) 绑定校验器和校验注解之间的关系  @Constraint(validatedBy = {ListValidConstraintValidator.class})
 */
public class ListValidConstraintValidator implements ConstraintValidator<ListValid,Integer> {

    private Set<Integer> set=new HashSet<>();
//    初始化方法
    @Override
    public void initialize(ListValid constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if (vals!=null){
            for (int val : vals) {
                set.add(val);
            }
        }
    }

//    是否符合判断
    /*
    integer 需要判断的值
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
